<?php
/**
 * Formular ACL
 * @author Tomas Srna
 * @package bakalarka
 */

class AclForm extends Form 
{
	public function __construct(Template $template, $values = NULL, 
		$action = NULL, $method = 'POST')
	{
		parent::__construct($template, $values, $action, $method);
		
		// name, label, [type], [class], [hint], [$options]
		$this->items = array(
			new Element('role', L::selected()->role, "text", 
						"required nospace", L::selected()->hintRole),
			new Element('resource', L::selected()->resource, "text", 
						"required nospace"),
			new Element('allow', L::selected()->action, "select", 
						"required", "", array(
							1 => L::selected()->allow,
							0 => L::selected()->deny,
						)),
		);
	}
	
	/**
	 * Pokial nam zakladne kontroly nestacia, mozme si pridat vlastne
	 * specificke pre dany formular
	 */
	public function validate()
	{
		parent::validate();
		
		$role_possible = array('@admin', '@user', '@anonymous');
		
		if(array_search($this->getValue('role'), $role_possible) === false &&
			NULL === UserModel::load(array(
				'username' => $this->getValue('role')))
		)
		{
			$this->setError('role', L::selected()->invalidFormat);
		}
		
		if(!$this->getError('resource') &&
			!preg_match('/^[*a-z0-9:_\\.-]+$/i', 
				$this->getValue('resource')))
		{
			$this->setError('resource', 
				sprintf(L::selected()->invalidFormat_s, 
					'a-z, A-Z, 0-9, _ - . : *'));
		}
		
		if(count($this->errors) > 0)
			return false;
		
		return true;
	}
}
